package com.lianzhihui.romeex.ui.account;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.base.AppConfig;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.database.DataBaseManager;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.TextUtil;
import com.lianzhihui.romeex.utils.UiUtils;
import com.lianzhihui.romeex.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 修改安全码
 * Author: RAMON
 * CreateDate: 2020/11/3 5:47 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ModSaftyActivity extends BaseActivity {

    @BindView(R.id.et_og_safty)
    EditText et_og_safty;
    @BindView(R.id.et_safty)
    EditText et_safty;
    @BindView(R.id.et_confirm)
    EditText et_confirm;

    @BindView(R.id.iv_eyes1)
    ImageView iv_eyes1;
    @BindView(R.id.iv_eyes2)
    ImageView iv_eyes2;

    String address;
    WalletInfo walletInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mod_safty_layout;
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.mod_password));
        Intent intent = getIntent();
        address = intent.getStringExtra(AppConfig.ADDRESS);
        if (TextUtils.isEmpty(address)) {
            toast("无地址");
            finish();
        }

        walletInfo = UserUtils.getUser(address);
        if (walletInfo == null) {
            toast("无效钱包");
            finish();
        }

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.bt_submit, R.id.iv_eyes1, R.id.iv_eyes2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_submit:

                //验证密码
                String inputPsw = et_og_safty.getText().toString();
                String newPsw = et_safty.getText().toString();
                String confirmPsw = et_confirm.getText().toString();

                if (walletInfo.equSafeCode(inputPsw)) {
                    //验证密码规则
                    if (!TextUtil.checkSaftyCode(newPsw)) {
                        toast(getString(R.string.six_password));
                        return;
                    }
                    //修改密码
                    WalletInfo wi = UserUtils.getUser(address);
                    if (newPsw.equals(confirmPsw)) {
                        //修改密码
                        wi.setSaftyCodeSample(newPsw);
                        DataBaseManager.up_WalletToDb(wi);
                        toast("修改完成");
                        finish();
                    } else {
                        toast("两次密码输入不一致");
                    }

                } else {
                    toast("密码错误");
                }

                break;
            case R.id.iv_eyes1:
                UiUtils.setTextEyes(iv_eyes1, et_safty);
                break;
            case R.id.iv_eyes2:
                UiUtils.setTextEyes(iv_eyes2, et_confirm);
                break;
            default:
                break;
        }

    }

}
